﻿var dialog;
var isNoCity = false;
var isNoDistrict = false;
$(function () {
    dialog = frameElement.dialog;
    $("#lblBED_NO").text(dialog.get('data').BED_NO);
    $("#lblPATIENT_NAME").text(dialog.get('data').PATIENT_NAME);

    $("#txtESCORT_AGE").ligerTextBox({ digits: true });

    $("#ddlESCORT_SEX").ligerComboBox({
        cancelable: false,
        width: 120,
        data: [
              { id: 1, text: "男" },
              { id: 2, text: "女" }
        ],
        valueFieldID: 'hdnESCORT_SEX',
        value: 1
    });

    $("#ddlKINSHIP").ligerComboBox({
        cancelable: false,
        width: 120,
        url: "/Escort/EscortPersonnel/GetBaseCommonDict?isAll=1&dictName=KINSHIP_DICT&ram" + Math.random(),
        valueField: "ITEM_CODE",
        textField: "ITEM_NAME",
        valueFieldID: 'hdnKINSHIP',
        value: -1
    });

    $("#ddlID_CARD_TYPE").ligerComboBox({
        cancelable: false,
        width: 120,
        url: "/Escort/EscortPersonnel/GetBaseCommonDict?dictName=ID_CARD_TYPE_DICT&ram" + Math.random(),
        valueField: "ITEM_CODE",
        textField: "ITEM_NAME",
        valueFieldID: 'hdnID_CARD_TYPE',
        onSuccess: function (data) {
            $("#ddlID_CARD_TYPE").ligerComboBox().setValue($("#ddlID_CARD_TYPE").ligerComboBox().findValueByText("居民身份证"));
        }
    });

    $("#ddlKEY_AREAS_DESC").ligerComboBox({
        cancelable: false,
        isMultiSelect: true,
        isShowCheckBox: true,
        width: 250,
        url: "/Escort/EscortPersonnel/GetBaseCommonDict?dictName=KEY_AREAS_DICT&ram" + Math.random(),
        valueField: "ITEM_CODE",
        textField: "ITEM_NAME",
        valueFieldID: 'hdnKEY_AREAS_DESC'
    });

    $("input[name=rdoOUT_COME_FLAG]").click(function (item) {
        $("[class^=divOUT_COME_FLAG]").hide();
        $(".divOUT_COME_FLAG" + $(this).val()).show();
    });

    $("input[name=rdoKEY_AREAS_FLAG]").click(function (item) {
        $("[id^=divKEY_AREAS_FLAG]").hide();
        $("#divKEY_AREAS_FLAG" + $(this).val()).show();
        $("#ddlKEY_AREAS_DESC").hide();
    });

    $("input[name=rdoABROAD_FLAG]").click(function (item) {
        $("[id^=divABROAD_FLAG]").hide();
        $("#divABROAD_FLAG" + $(this).val()).show();
    });

    $("#txtSTART_DATE").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 120,
        cancelable: false
    });

    $("#txtCOME_FROM_DATE").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 105,
        cancelable: false
    });

    $("#txtENTRY_DATE").ligerDateEditor({
        showTime: false,
        format: "yyyy-MM-dd",
        width: 90,
        cancelable: false
    });

    $.getJSON("/Json/Districts.json?ram=" + Math.random(), function (data) {
        var provinceJson = [];
        var cityJson = [];
        var districtJson = [];
        var provinceData = data["100000"];
        provinceJson.push({ id: -1, text: "--省份--" });
        for (var key in provinceData) {
            provinceJson.push({ id: key, text: provinceData[key] });
        }
        $("#ddlProvince").ligerComboBox({
            width: 120,
            cancelable: false,
            selectBoxWidth: 120,
            data: provinceJson,
            valueFieldID: "hdnProvince",
            onSelected: function (value) {
                var cityData = data[value];
                cityJson = [];
                cityJson.push({ id: -1, text: "--城市--" });
                for (var key in cityData) {
                    cityJson.push({ id: key, text: cityData[key] });
                }
                if (value != -1 && cityJson.length == 1) {
                    isNoCity = true;
                }
                else {
                    isNoCity = false;
                }
                $("#ddlCity").ligerComboBox().setData(cityJson);
                $("#ddlCity").ligerComboBox().setValue("-1");
            }
        });

        $("#ddlCity").ligerComboBox({
            width: 100,
            selectBoxWidth: 100,
            cancelable: false,
            data: cityJson,
            valueFieldID: "hdnCity",
            onSelected: function (value) {
                var districtData = data[value];
                districtJson = [];
                districtJson.push({ id: -1, text: "--地区--" });
                for (var key in districtData) {
                    districtJson.push({ id: key, text: districtData[key] });
                }
                if ((value != -1 && districtJson.length == 1) || isNoCity) {
                    isNoDistrict = true;
                }
                else {
                    isNoDistrict = false;
                }
                $("#ddlDistrict").ligerComboBox().setData(districtJson);
                $("#ddlDistrict").ligerComboBox().setValue("-1");
            }
        });

        $("#ddlDistrict").ligerComboBox({
            width: 120,
            selectBoxWidth: 120,
            cancelable: false,
            data: districtJson,
            valueFieldID: "hdnDistrict",
            onSelected: function (value) {
            }
        });

        bindEscortPersonnel();
    });
});

function bindEscortPersonnel() {
    if (!IsEmpty($("#hdnId").val())) {
        $.ajax({
            url: "/Escort/EscortPersonnel/GetEscortInfoByID?ram" + Math.random(),
            type: "post",
            data: { id: $("#hdnId").val() },
            success: function (data) {
                var r = $.parseJSON(data);
                if (r.Result == 1) {
                    if ($.parseJSON(r.Body).length == 0) return;
                    var body = $.parseJSON(r.Body)[0];
                    $("#txtSTART_DATE").val(body.START_DATE.split(' ')[0].replace(/\//g, "-"));
                    $("#txtESCORT_NAME").val(body.ESCORT_NAME);
                    $("#txtESCORT_AGE").val(body.ESCORT_AGE);
                    $("#txtESCORT_PHONE").val(body.ESCORT_PHONE);
                    if (body.ADDR_CODE != "") {
                        var province = body.ADDR_CODE.substring(0, 2) + "0000";
                        var city = body.ADDR_CODE.substring(0, 4) + "00";
                        $("#ddlProvince").ligerComboBox().setValue(province);
                        var t1 = window.setInterval(function () {
                            if ($("#ddlProvince").val() != "-1") {
                                if (IsEmpty($("#ddlCity").ligerComboBox().findTextByValue(city))) {
                                    $("#ddlCity").ligerComboBox().setValue(-1);
                                }
                                else {
                                    $("#ddlCity").ligerComboBox().setValue(city);
                                }
                                window.clearInterval(t1);
                            }
                        }, 100);
                        var t2 = window.setInterval(function () {
                            if ($("#ddlCity").val() != "-1") {
                                if (IsEmpty($("#ddlDistrict").ligerComboBox().findTextByValue(body.ADDR_CODE))) {
                                    $("#ddlDistrict").ligerComboBox().setValue(-1);
                                }
                                else {
                                    $("#ddlDistrict").ligerComboBox().setValue(body.ADDR_CODE);
                                }
                                window.clearInterval(t2);
                            }
                        }, 100);
                    }
                    $("#ddlESCORT_SEX").ligerComboBox().setValue(body.ESCORT_SEX);
                    $("#ddlKINSHIP").ligerComboBox().setValue(body.KINSHIP);
                    $("#ddlID_CARD_TYPE").ligerComboBox().setValue(body.ID_CARD_TYPE);
                    $("#txtID_CARD_NO").val(body.ID_CARD_NO);
                    $("#txtADDR_DETAIL").val(body.ADDR_DETAIL);
                    $('input:radio[name="rdoESCORT_TYPE"][value=' + body.ESCORT_TYPE + ']').attr('checked', 'true').click();
                    $('input:radio[name="rdoHEALTH_CODE_TYPE"][value=' + body.HEALTH_CODE_TYPE + ']').attr('checked', 'true').click();
                    $('input:radio[name="rdoOUT_COME_FLAG"][value=' + body.OUT_COME_FLAG + ']').attr('checked', 'true').click();
                    $('input:radio[name="rdoKEY_AREAS_FLAG"][value=' + body.KEY_AREAS_FLAG + ']').attr('checked', 'true').click();
                    $('input:radio[name="rdoDIGESTION_FLAG"][value=' + body.DIGESTION_FLAG + ']').attr('checked', 'true').click();
                    $('input:radio[name="rdoEPIDEMIC_FLAG"][value=' + body.EPIDEMIC_FLAG + ']').attr('checked', 'true').click();
                    $('input:radio[name="rdoCLINIC_SYMP_FLAG"][value=' + body.CLINIC_SYMP_FLAG + ']').attr('checked', 'true').click();
                    $("#txtCOME_FROM_DATE").val(body.COME_FROM_DATE.split(' ')[0].replace(/\//g, "-"));
                    $("#ddlKEY_AREAS_DESC").ligerComboBox().setValue(body.KEY_AREAS_DESC);
                    $('input:radio[name="rdoABROAD_FLAG"][value=' + body.ABROAD_FLAG + ']').attr('checked', 'true').click();
                    $("#txtENTRY_DATE").val(body.ENTRY_DATE.split(' ')[0].replace(/\//g, "-"));
                    $("#txtABROAD_COUNTRY").val(body.ABROAD_COUNTRY);
                    if (body.REPLACE_FLAG == 1) $("#chkREPLACE_FLAG").attr('checked', 'true');
                }
                else if (r.Result == 0) {
                    $.ligerDialog.error(r.Body);
                }
                else {
                    $.ligerDialog.error("程序错误");
                }
            }
        });
    }
    else {
        $("#ddlProvince").ligerComboBox().setValue("350000");
        var t1 = window.setInterval(function () {
            if ($("#ddlProvince").val() != "-1") {
                $("#ddlCity").ligerComboBox().setValue("350500");
                window.clearInterval(t1);
            }
        }, 100);
    }
}

function save() {
    var id = $("#hdnId").val();
    var START_DATE = $("#txtSTART_DATE").val();
    var ESCORT_NAME = $("#txtESCORT_NAME").val();
    var ESCORT_AGE = $("#txtESCORT_AGE").val();
    var ESCORT_SEX = $("#hdnESCORT_SEX").val();
    var ID_CARD_TYPE = $("#hdnID_CARD_TYPE").val();
    var ID_CARD_NO = $("#txtID_CARD_NO").val();
    var ESCORT_PHONE = $("#txtESCORT_PHONE").val();
    var KINSHIP = $("#hdnKINSHIP").val();
    var Province = $("#ddlProvince").ligerComboBox().getText();
    var City = $("#ddlCity").ligerComboBox().getText();
    var District = $("#ddlDistrict").ligerComboBox().getText();
    var ADDR_CODE = $("#hdnDistrict").val();
    var ADDR_DETAIL = $("#txtADDR_DETAIL").val();
    var ESCORT_TYPE = $('input[name="rdoESCORT_TYPE"]:checked').val();
    var HEALTH_CODE_TYPE = $('input[name="rdoHEALTH_CODE_TYPE"]:checked').val();
    var OUT_COME_FLAG = $('input[name="rdoOUT_COME_FLAG"]:checked').val();
    var COME_FROM_DATE = $("#txtCOME_FROM_DATE").val();
    if (OUT_COME_FLAG != 1 && OUT_COME_FLAG != 2) COME_FROM_DATE = "";
    var KEY_AREAS_FLAG = $('input[name="rdoKEY_AREAS_FLAG"]:checked').val();
    var KEY_AREAS_DESC = $("#ddlKEY_AREAS_DESC").ligerComboBox().getValue();
    if (KEY_AREAS_FLAG != 1) KEY_AREAS_DESC = "";
    var DIGESTION_FLAG = $('input[name="rdoDIGESTION_FLAG"]:checked').val();
    var EPIDEMIC_FLAG = $('input[name="rdoEPIDEMIC_FLAG"]:checked').val();
    var CLINIC_SYMP_FLAG = $('input[name="rdoCLINIC_SYMP_FLAG"]:checked').val();
    var REPLACE_FLAG = $("#chkREPLACE_FLAG").is(':checked');
    var ABROAD_FLAG = $('input[name="rdoABROAD_FLAG"]:checked').val();
    var ENTRY_DATE = $("#txtENTRY_DATE").val();
    var ABROAD_COUNTRY = $("#txtABROAD_COUNTRY").val();
    if (ABROAD_FLAG != 1) {
        ENTRY_DATE = "";
        ABROAD_COUNTRY = "";
    }
    var PID = dialog.get('data').PID;
    var VID = dialog.get('data').VID;
    var NURSE_CELL_CODE = dialog.get('data').NURSE_CELL_CODE;

    if (IsEmpty(ESCORT_NAME)) {
        $.ligerDialog.error("姓名不能为空");
        $("#txtESCORT_NAME").focus();
        return;
    }

    if (IsEmpty(ESCORT_AGE)) {
        $.ligerDialog.error("年龄不能为空");
        $("#txtESCORT_AGE").focus();
        return;
    }

    if (IsEmpty(ID_CARD_NO)) {
        $.ligerDialog.error("证件号不能为空");
        $("#txtID_CARD_NO").focus();
        return;
    }

    if (IsEmpty(ESCORT_PHONE)) {
        $.ligerDialog.error("联系方式不能为空");
        $("#txtESCORT_PHONE").focus();
        return;
    }
    if (KINSHIP == "-1") {
        $.ligerDialog.error("请选择患者关系");
        $("#txtESCORT_NAME").focus();
        return;
    }
    if (ADDR_CODE == -1) {
        if (isNoCity) {
            ADDR_CODE = $("#hdnProvince").val();
        }
        else if (isNoDistrict) {
            ADDR_CODE = $("#hdnCity").val();
        }
        else {
            $.ligerDialog.error("请填写完整籍贯");
            $("#ddlDistrict").focus();
            return;
        }
    }
    if (IsEmpty(ADDR_DETAIL)) {
        $.ligerDialog.error("现住地址不能为空");
        $("#txtADDR_DETAIL").focus();
        return;
    }

    if (OUT_COME_FLAG == 2 && IsEmpty(COME_FROM_DATE)) {
        $.ligerDialog.error("来泉时间不能为空");
        $("#txtCOME_FROM_DATE").focus();
        return;
    }

    var ADDR_NAME = Province + (isNoCity ? "" : City) + (isNoDistrict ? "" : District);

    var jsonInfo = {
        START_DATE: START_DATE,
        ESCORT_NAME: ESCORT_NAME,
        ESCORT_AGE: ESCORT_AGE,
        ESCORT_SEX: ESCORT_SEX,
        ID_CARD_TYPE: ID_CARD_TYPE,
        ID_CARD_NO: ID_CARD_NO,
        ESCORT_PHONE: ESCORT_PHONE,
        KINSHIP: KINSHIP,
        ADDR_CODE: ADDR_CODE,
        ADDR_NAME: ADDR_NAME,
        ADDR_DETAIL: ADDR_DETAIL,
        ESCORT_TYPE: ESCORT_TYPE,
        HEALTH_CODE_TYPE: HEALTH_CODE_TYPE,
        OUT_COME_FLAG: OUT_COME_FLAG,
        COME_FROM_DATE: COME_FROM_DATE,
        KEY_AREAS_FLAG: KEY_AREAS_FLAG,
        KEY_AREAS_DESC: KEY_AREAS_DESC,
        DIGESTION_FLAG: DIGESTION_FLAG,
        EPIDEMIC_FLAG: EPIDEMIC_FLAG,
        CLINIC_SYMP_FLAG: CLINIC_SYMP_FLAG,
        REPLACE_FLAG: REPLACE_FLAG,
        ABROAD_FLAG: ABROAD_FLAG,
        ENTRY_DATE: ENTRY_DATE,
        ABROAD_COUNTRY: ABROAD_COUNTRY,
        PID: PID,
        VID: VID,
        NURSING_UNIT: NURSE_CELL_CODE
    };

    $.ajax({
        url: "/Escort/EscortPersonnel/Save",
        type: "post",
        data: {
            id: id,
            jsonInfo: JSON.stringify(jsonInfo)
        },
        success: function (r) {
            var r = $.parseJSON(r);
            if (r.Result == 1) {
                $.ligerDialog.success("保存成功", "",
                    function () {
                        var chkPrintEscortCard = $("#chkPrintEscortCard").is(':checked');
                        if (chkPrintEscortCard) {
                            parent.$("#hdnESCORT_ID").val(r.Body);
                            parent.$("#btnPrintEscortCard").click();
                            closeDialog();
                        }
                        else {
                            parent.$("#btnSearch").click();
                            closeDialog();
                        }
                    }, { allowClose: false });
            }
            else if (r.Result == 0) {
                $.ligerDialog.error(r.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}